home *** CD-ROM | disk | FTP | other *** search
/ Risc World 5 / Risc World 5.iso / SOFTWARE / Issue5 / PD / DIRSYNC / LegalStuff / ccres / s / Library
Text File  |  2004-10-31  |  7KB  |  383 lines

  1. ; Library.s
  2. ; $Id: Library.s,v 1.3 2004/10/31 19:57:30 joty Exp $
  3. ;
  4. ; Copyright (c) 2003-2004 Dave Appleby / John Tytgat
  5. ;
  6. ; This file is part of CCres.
  7. ;
  8. ; CCres is free software; you can redistribute it and/or modify
  9. ; it under the terms of the GNU General Public License as published by
  10. ; the Free Software Foundation; either version 2 of the License, or
  11. ; (at your option) any later version.
  12. ;
  13. ; CCres is distributed in the hope that it will be useful,
  14. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. ; GNU General Public License for more details.
  17. ;
  18. ; You should have received a copy of the GNU General Public License
  19. ; along with CCres; if not, write to the Free Software
  20. ; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21.  
  22. ; need to include all recursively called headers here
  23. ; until assembler (as) can do conditional includes:-
  24.  
  25.     GET oslib.Hdr.Types
  26.     GET oslib.Hdr.OS
  27.     GET oslib.Hdr.FileSwitch
  28.     GET oslib.Hdr.OSArgs
  29.     GET oslib.Hdr.OSFile
  30.     GET oslib.Hdr.OSFind
  31.     GET oslib.Hdr.OSFSControl
  32.     GET oslib.Hdr.Territory
  33.     GET oslib.Hdr.Wimp
  34.  
  35.     EXPORT    my_strcpy
  36.     EXPORT    my_strcpy0d
  37.     EXPORT    my_strncpy0d
  38.     EXPORT    __stricmp
  39.     EXPORT    __strnicmp
  40.     EXPORT    __atoi
  41.     EXPORT    my_osfile_delete
  42.     EXPORT    my_osfile_exists
  43.     EXPORT    my_osfile_filesize
  44.     EXPORT    my_osfile_filetype
  45.     EXPORT    my_osfile_load
  46.     EXPORT    my_osfscontrol_count_objects
  47.  
  48.     IMPORT    achProgName
  49.  
  50. ; to assemble for 32bit code with ObjAsm use -Apcs 3/32bit (sets {CONFIG}=32)
  51. ; these MACRO's sort out flags by assuming the caller saves flags in 32bit
  52. ; (restoring flags isn't easy in 32bit mode when condition flags are specified!)
  53.  
  54. ; sregs is used to pass register list to function entry/exit macro's
  55. ; eg
  56. ; FNENTRY/FNEXIT                                    use lr for return
  57. ; sregs SETS lr    FNENTRY $sregs FNEXIT  $sregs    push lr
  58. ; sregs SETS r0,r1 FNENTRY $sregs FNEXIT $sregs     push r0,r1,lr
  59.  
  60.     GBLS    sregs
  61.  
  62.   MACRO
  63.   FNENTRY $regs
  64.   [ "$regs"<>""
  65.     [ "$regs"="lr"
  66.     STMFD    sp!, {lr}
  67.     |
  68.     STMFD    sp!, {$regs, lr}
  69.     ]
  70.   ]
  71.   MEND
  72.  
  73.   MACRO
  74.   FNEXIT $flags, $regs
  75.   [ {CONFIG}=32
  76.     [ "$regs"=""
  77.     MOV$flags    pc, lr
  78.     |
  79.       [ "$regs"="lr"
  80.     LDM$flags.FD    sp!, {pc}
  81.       |
  82.     LDM$flags.FD    sp!, {$regs, pc}
  83.       ]
  84.     ]
  85.   |
  86.     [ "$regs"=""
  87.     MOV$flags.S    pc, lr
  88.     |
  89.       [ "$regs"="lr"
  90.     LDM$flags.FD    sp!, {pc}^
  91.       |
  92.     LDM$flags.FD    sp!, {$regs, pc}^
  93.       ]
  94.     ]
  95.   ]
  96.   MEND
  97.  
  98.  
  99.         GBLA    ReportOK
  100. ReportOK    SETA    Wimp_ErrorBoxShortTitle + Wimp_ErrorBoxNoPrompt + Wimp_ErrorBoxOKIcon
  101.  
  102.  
  103.     AREA |C$$Code|,CODE,READONLY
  104.  
  105. ; int my_strcpy(PSTR to, PSTR from);
  106. ; same as string.h, but returns strlen
  107. ; entry        exit
  108. ; r0=to        r0=strlen (excluding term)
  109. ; r1=from
  110.  
  111. my_strcpy
  112.  
  113.     FNENTRY
  114.     ADD    r3, r1, #1
  115.  
  116. my_strcpy_loop
  117.  
  118.     LDRB    r2, [r1], #1
  119.     STRB    r2, [r0], #1
  120.     CMPS    r2, #0x00
  121.     BNE    my_strcpy_loop
  122.  
  123.     SUB    r0, r1, r3
  124.     FNEXIT
  125.  
  126. ; int my_strcpy0d(PSTR to, PSTR from);
  127. ; same as string.h, but handles 0x0d terminator and returns strlen
  128. ; entry        exit
  129. ; r0=to        r0=strlen (excluding term)
  130. ; r1=from
  131.  
  132. my_strcpy0d
  133.  
  134.     FNENTRY
  135.     ADD    r3, r1, #1
  136.  
  137. my_strcpy0d_loop
  138.  
  139.     LDRB    r2, [r1], #1
  140.     STRB    r2, [r0], #1
  141.     CMPS    r2, #0x00
  142.     CMPNES    r2, #0x0d
  143.     BNE    my_strcpy0d_loop
  144.  
  145.     MOV    r2, #0x00        ; change possible 0x0d term to 0x00
  146.     STRB    r2, [r0, #-1]
  147.     SUB    r0, r1, r3
  148.     FNEXIT
  149.  
  150.  
  151. ; void my_strncpy0d(PSTR to, PSTR from, int max);
  152. ; same as string.h, but handles 0x0d terminator and void return
  153. ; needs an extra register to return a useful value
  154. ; entry        exit
  155. ; r0=to
  156. ; r1=from
  157. ; r2=max
  158.  
  159. my_strncpy0d
  160.  
  161.     FNENTRY
  162.  
  163. my_strncpy0d_loop
  164.  
  165.     LDRB    r3, [r1], #1
  166.     STRB    r3, [r0], #1
  167.     SUBS    r2, r2, #1
  168.     CMPNES    r3, #0x00
  169.     CMPNES    r3, #0x0d
  170.     BNE    my_strncpy0d_loop
  171.  
  172.     MOV    r2, #0x00
  173.     CMPS    r3, #' '
  174.     STRLOB    r2, [r0, #-1]    ; change possible 0x0d term to 0x00
  175.     STRHSB    r2, [r0, #0]    ; add terminator to fixed length string
  176.     FNEXIT
  177.  
  178.  
  179. ; int __stricmp(PSTR p, PSTR q);
  180. ; on entry    on exit
  181. ; r0=string 1    r0=0 if equal
  182. ; r1=string 2
  183.  
  184. __stricmp
  185.  
  186.     FNENTRY
  187.     MOV    r3, #Territory_IgnoreCase
  188.     MOV    r2, r1
  189.     MOV    r1, r0
  190.     MOV    r0, #Territory_Current
  191.     SWI    Territory_Collate
  192.     FNEXIT
  193.  
  194.  
  195. ; int __strnicmp(PSTR p, PSTR q, int n);
  196. ; assumes n is always >= 1
  197. ; on entry    on exit
  198. ; r0=string 1    r0=0 if equal
  199. ; r1=string 2
  200. ; r2=n
  201.  
  202. __strnicmp
  203. sregs    SETS    "r4"
  204.  
  205.     FNENTRY    $sregs
  206.     MOV    r3, r0
  207.     MOV    r0, #-1
  208.     SWI    Territory_UpperCaseTable
  209.     MOV    lr, r0
  210.  
  211. __strnicmp_loop
  212.  
  213.     LDRB    r0, [r1], #1
  214.     LDRB    r4, [r3], #1
  215.     LDRB    r0, [lr, r0]
  216.     LDRB    r4, [lr, r4]
  217.     SUBS    r0, r4, r0
  218.     FNEXIT    NE, $sregs
  219.     SUBS    r2, r2, #1
  220.     CMPNES    r4, #0
  221.     BNE    __strnicmp_loop
  222.     FNEXIT    , $sregs
  223.  
  224.  
  225. ; unsigned int __atoi(PSTR * pszNumber);
  226. ; returns term pointer in *pszNumber
  227. ; OS_ReadUnsigned is far better than atoi library function!!
  228. ; on entry    on exit
  229. ; r0=&str    r0=number
  230.  
  231. __atoi
  232. sregs    SETS    "lr"
  233.  
  234.     FNENTRY    $sregs
  235.     MOV    lr, r0
  236.     LDR    r1, [lr]
  237.     LDRB    r3, [r1]        ; check for -ve
  238.     CMPS    r3, #'-'
  239.     ADDEQ    r1, r1, #1
  240.  
  241.     MOV    r0, #10
  242.     SWI    XOS_ReadUnsigned
  243.     MOVVS    r0, #0
  244.     MOVVC    r0, r2
  245.     STRVC    r1, [lr]        ; address of terminator
  246.  
  247.     CMPS    r3, #'-'        ; adjust -ve
  248.     RSBEQ    r0, r0, #0
  249.  
  250.     FNEXIT    ,$sregs
  251.  
  252.  
  253. ; BOOL my_osfile_delete(PSTR pszFile);
  254. ; on entry    on exit
  255. ; r0=filepath    r0=True if deleted
  256.  
  257. my_osfile_delete
  258. sregs    SETS    "r4-r5"
  259.  
  260.     FNENTRY    $sregs
  261.     MOV    r1, r0
  262.     MOV    r0, #6
  263.     SWI    XOS_File
  264.     MOVVS    r0, #False
  265.     MOVVC    r0, #True
  266.     FNEXIT    , $sregs
  267.  
  268.  
  269. ; fileswitch_object_type my_osfile_exists(PSTR pszFile);
  270. ; on entry    on exit
  271. ; r0=filepath    r0=True if exists
  272.  
  273. my_osfile_exists
  274. sregs    SETS    "r4-r5"
  275.  
  276.     FNENTRY    $sregs
  277.     MOV    r1, r0
  278.     MOV    r0, #17
  279.     SWI    XOS_File
  280.     FNEXIT    , $sregs
  281.  
  282.  
  283. ; int my_osfile_filesize(PSTR pszFile);
  284. ; on entry    on exit
  285. ; r0=file    r0=filesize
  286.  
  287. my_osfile_filesize
  288. sregs    SETS    "r4-r5"
  289.  
  290.     FNENTRY    $sregs
  291.     MOV    r1, r0
  292.     MOV    r0, #17
  293.     SWI    XOS_File
  294.     MOVVS    r4, #0
  295.     CMPS    r0, #OSFile_IsFile
  296.     MOVNE    r0, #0
  297.     MOVEQ    r0, r4
  298.     FNEXIT    , $sregs
  299.  
  300.  
  301. ; bits my_osfile_filetype(PSTR pszFile);
  302. ; on entry    on exit
  303. ; r0=file    r0=type
  304.  
  305. my_osfile_filetype
  306. sregs    SETS    "r4-r6"
  307.  
  308.     FNENTRY    $sregs
  309.     MOV    r1, r0
  310.     MOV    r0, #23
  311.     SWI    XOS_File
  312.     CMPS    r0, #0
  313.     MVNEQ    r0, #0
  314.     MOVNE    r0, r6
  315.     FNEXIT    , $sregs
  316.  
  317.  
  318. ; int my_osfile_load(PSTR pszFile, PSTR pszBuff, int cbBuff);
  319. ; on entry    on exit
  320. ; r0=file    r0=bytes read
  321. ; r1=buff
  322. ; r2=cbBuff
  323.  
  324. my_osfile_load
  325. sregs    SETS    "r4-r5"
  326. pszBuff    RN    ip
  327. cbBuff    RN    lr
  328.  
  329.     FNENTRY    $sregs
  330.     MOV    pszBuff, r1
  331.     MOV    cbBuff, r2
  332.  
  333.     MOV    r1, r0
  334.     MOV    r0, #17
  335.     SWI    XOS_File
  336.     FNEXIT    VS, $sregs
  337.  
  338.     CMPS    r0, #OSFile_IsFile
  339.     FNEXIT    NE, $sregs
  340.  
  341.     CMPS    r4, cbBuff
  342.     ADRGT    r0, osfile_load_smallbuff
  343.     BGT    osfile_load_error
  344.  
  345.     MOV    r0, #16
  346. ; r1 is file path
  347.     MOV    r2, pszBuff
  348.     MOV    r3, #0
  349.     SWI    XOS_File
  350.     MOVVC    r0, r4
  351.     FNEXIT    VC, $sregs
  352.  
  353. osfile_load_error
  354.  
  355.     MOV    r1, #ReportOK
  356.     LDR    r2, pachProgName
  357.     SWI    Wimp_ReportError
  358.     FNEXIT    , $sregs
  359.  
  360. pachProgName        DCD    achProgName
  361. osfile_load_smallbuff    DCD    0
  362.             DCB    "osfile_load buffer too small",0
  363.             ALIGN
  364.  
  365. ; int osfscontrol_count_objects(PSTR pszDir);
  366. ; on entry    on exit
  367. ; r0=dir    r0=Object count
  368.  
  369. my_osfscontrol_count_objects
  370.  
  371.     FNENTRY
  372.     MOV    r1, r0
  373.     MOV    r0, #28
  374.     MOV    r2, #0
  375.     MOV    r3, #0
  376.     SWI    XOS_FSControl
  377.     MOVVS    r0, #-1
  378.     MOVVC    r0, r3
  379.     FNEXIT
  380.  
  381.  
  382.     END
  383.